<!DOCTYPE HTML>
<html>

<head>
	<link rel="bookmark"  type="image/x-icon"  href="/img/logo_miccall.png"/>
	<link rel="shortcut icon" href="/img/logo_miccall.png">
	
			    <title>
    Ame雨
    </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="/amelin-ting/css/mic_main.css" />
    <link rel="stylesheet" href="/amelin-ting/css/dropdownMenu.css" />
    <meta name="keywords" content="Ame雨" />
    
    	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	 
    <noscript>
        <link rel="stylesheet" href="/amelin-ting/css/noscript.css" />
    </noscript>
    <style type="text/css">
        body:before {
          content: ' ';
          position: fixed;
          top: 0;
          background: url('/amelin-ting/img/bg.jpg') center 0 no-repeat;
          right: 0;
          bottom: 0;
          left: 0;
          background-size: cover; 
        }
    </style>

			    
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script async type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


    <script src="/amelin-ting/js/jquery.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrollex.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrolly.min.js"></script>
    <script src="/amelin-ting/js/skel.min.js"></script>
    <script src="/amelin-ting/js/util.js"></script>
    <script src="/amelin-ting/js/main.js"></script>
	
<meta name="generator" content="Hexo 5.4.0"></head>
    
		
<!-- Layouts -->



<!--  代码渲染  -->
<link rel="stylesheet" href="/css/prism_coy.css" />
<link rel="stylesheet" href="/css/typo.css" />
<!-- 文章页 -->
<body class="is-loading">
    <!-- Wrapper 外包 s-->
    <div id="wrapper" class="fade-in">
        <!-- Intro 头部显示 s -->
        <!-- Intro 头部显示 e -->
        <!-- Header 头部logo start -->
        <header id="header">
    <a href="/amelin-ting/" class="logo">Art Space</a>
</header>
        <!-- Nav 导航条 start -->
        <nav id="nav" class="special" >
            <ul class="menu links" >
			<!-- Homepage  主页  --> 
			<li >
	            <a href="/amelin-ting/" rel="nofollow">主页</a>
	        </li>
			<!-- categories_name  分类   --> 
	        
	        <li class="active">
	            <a href="#s1">分类</a>
	                    <ul class="submenu">
	                        <li>
	                        <a class="category-link" href="/amelin-ting/categories/Go/">Go</a></li><li><a class="category-link" href="/amelin-ting/categories/django/">django</a></li><li><a class="category-link" href="/amelin-ting/categories/drf/">drf</a></li><li><a class="category-link" href="/amelin-ting/categories/gin/">gin</a></li><li><a class="category-link" href="/amelin-ting/categories/go/">go</a></li><li><a class="category-link" href="/amelin-ting/categories/linux/">linux</a></li><li><a class="category-link" href="/amelin-ting/categories/mysql/">mysql</a></li><li><a class="category-link" href="/amelin-ting/categories/python/">python</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">内网穿透</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%89%8D%E7%AB%AF/">前端</a>
	                    </ul>
	        </li>
	        
	        <!-- archives  归档   --> 
	        
	        
		        <!-- Pages 自定义   -->
		        
		        <li>
		            <a href="/about/" title="简历">
		                简历
		            </a>
		        </li>
		        
		        <li>
		            <a href="/group/" title="团队">
		                团队
		            </a>
		        </li>
		        
		        <li>
		            <a href="/gallery/" title="图库">
		                图库
		            </a>
		        </li>
		        
		        <li>
		            <a href="/tag/" title="标签">
		                标签
		            </a>
		        </li>
		        


            </ul>
            <!-- icons 图标   -->
			<ul class="icons">
                    
                    <li>
                        <a title="github" href="https://github.com/miccall" target="_blank" rel="noopener">
                            <i class="icon fa fa-github"></i>
                        </a>
                    </li>
                    
                    <li>
                        <a title="500px" href="http://500px.com" target="_blank" rel="noopener">
                            <i class="icon fa fa-500px"></i>
                        </a>
                    </li>
                    
			</ul>
</nav>

        <div id="main" >
            <div class ="post_page_title_img" style="height: 25rem;background-image: url(https://img2.baidu.com/it/u=1642844641,1835304391&amp;fm=26&amp;fmt=auto);background-position: center; background-repeat:no-repeat; background-size:cover;-moz-background-size:cover;overflow:hidden;" >
                <a href="#" style="padding: 4rem 4rem 2rem 4rem ;"><h2 >django模板层</h2></a>
            </div>
            <!-- Post -->
            <div class="typo" style="padding: 3rem;">
                <h2 id="一、模板语法传值"><a href="#一、模板语法传值" class="headerlink" title="一、模板语法传值"></a>一、模板语法传值</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 什么是模板语法传值</span><br><span class="line">	我们在后端写的python数据类型可以利用模板语法传到前端</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    ps: 经验之谈  &#123;&#123;&#125;&#125;:变量相关    &#123;%%&#125;:逻辑相关</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line"> </span><br><span class="line"><span class="number">2.</span> 代码演示</span><br><span class="line">	<span class="comment"># 后端代码</span></span><br><span class="line">    	<span class="keyword">from</span> django.shortcuts <span class="keyword">import</span> render</span><br><span class="line">        <span class="comment"># Create your views here.</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">request</span>):</span></span><br><span class="line">            s = <span class="string">&#x27;yxx想奔现&#x27;</span></span><br><span class="line">            i = <span class="number">123456</span></span><br><span class="line">            f = <span class="number">88.88</span></span><br><span class="line">            l = [<span class="string">&#x27;叶叶&#x27;</span>, <span class="string">&#x27;晨晨&#x27;</span>, <span class="string">&#x27;诚诚&#x27;</span>]</span><br><span class="line">            b = <span class="literal">True</span></span><br><span class="line">            t = (<span class="number">111</span>, <span class="number">222</span>, <span class="number">333</span>, <span class="number">444</span>)</span><br><span class="line">            d = &#123;<span class="string">&#x27;username&#x27;</span>: <span class="string">&#x27;jason&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;info&#x27;</span>: <span class="string">&#x27;这个人有点意思&#x27;</span>&#125;</span><br><span class="line">            se = &#123;<span class="string">&#x27;晶晶&#x27;</span>, <span class="string">&#x27;瑶瑶&#x27;</span>, <span class="string">&#x27;嘤嘤&#x27;</span>&#125;</span><br><span class="line"></span><br><span class="line">            <span class="function"><span class="keyword">def</span> <span class="title">func</span>():</span></span><br><span class="line">                <span class="built_in">print</span>(<span class="string">&#x27;你好&#x27;</span>)</span><br><span class="line">                <span class="keyword">return</span> <span class="string">&#x27;func&#x27;</span></span><br><span class="line"></span><br><span class="line">            <span class="class"><span class="keyword">class</span> <span class="title">Myclass</span>():</span></span><br><span class="line">                <span class="function"><span class="keyword">def</span> <span class="title">get_self</span>(<span class="params">self</span>):</span></span><br><span class="line">                    <span class="keyword">return</span> <span class="string">&#x27;self&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">                @staticmethod</span></span><br><span class="line">                <span class="function"><span class="keyword">def</span> <span class="title">get_func</span>():</span></span><br><span class="line">                    <span class="keyword">return</span> <span class="string">&#x27;get_func&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">                @classmethod</span></span><br><span class="line">                <span class="function"><span class="keyword">def</span> <span class="title">get_class</span>(<span class="params">cls</span>):</span></span><br><span class="line">                    <span class="keyword">return</span> <span class="string">&#x27;get_class&#x27;</span></span><br><span class="line">                </span><br><span class="line">			   <span class="comment"># 对象被展示到html页面上 就类似于执行了打印操作也会触发__str__方法</span></span><br><span class="line">                <span class="function"><span class="keyword">def</span> <span class="title">__str__</span>(<span class="params">self</span>):</span></span><br><span class="line">                    <span class="keyword">return</span> <span class="string">&#x27;奔现奔现&#x27;</span></span><br><span class="line"></span><br><span class="line">            obj = Myclass()</span><br><span class="line"></span><br><span class="line">            <span class="keyword">return</span> render(request, <span class="string">&#x27;index.html&#x27;</span>, <span class="built_in">locals</span>())</span><br><span class="line">        </span><br><span class="line">        <span class="comment"># 前端语法</span></span><br><span class="line">            &lt;p&gt;&#123;&#123; n &#125;&#125;&lt;/p&gt;</span><br><span class="line">            &lt;p&gt;&#123;&#123; f &#125;&#125;&lt;/p&gt;</span><br><span class="line">            &lt;p&gt;&#123;&#123; s &#125;&#125;&lt;/p&gt;</span><br><span class="line">            &lt;p&gt;&#123;&#123; b &#125;&#125;&lt;/p&gt;</span><br><span class="line">            &lt;p&gt;&#123;&#123; l &#125;&#125;&lt;/p&gt;</span><br><span class="line">            &lt;p&gt;&#123;&#123; d &#125;&#125;&lt;/p&gt;</span><br><span class="line">            &lt;p&gt;&#123;&#123; t &#125;&#125;&lt;/p&gt;</span><br><span class="line">            &lt;p&gt;&#123;&#123; se &#125;&#125;&lt;/p&gt;</span><br><span class="line">            &lt;p&gt;传递函数名会自动加括号调用 但是模版语法不支持给函数传额外的参数:&#123;&#123; func &#125;&#125;&lt;/p&gt;</span><br><span class="line">            &lt;p&gt;传类名的时候也会自动加括号调用(实例化)&#123;&#123; MyClass &#125;&#125;&lt;/p&gt;</span><br><span class="line">            &lt;p&gt;内部能够自动判断出当前的变量名是否可以加括号调用 如果可以就会自动执行  针对的是函数名和类名&lt;/p&gt;</span><br><span class="line">            &lt;p&gt;&#123;&#123; obj &#125;&#125;&lt;/p&gt;</span><br><span class="line">            &lt;p&gt;&#123;&#123; obj.get_self &#125;&#125;&lt;/p&gt;</span><br><span class="line">            &lt;p&gt;&#123;&#123; obj.get_func &#125;&#125;&lt;/p&gt;</span><br><span class="line">            &lt;p&gt;&#123;&#123; obj.get_class &#125;&#125;&lt;/p&gt;</span><br><span class="line"></span><br><span class="line">       </span><br><span class="line"><span class="comment"># django模版语法的取值 是固定的格式 只能采用“句点符” .</span></span><br><span class="line">&lt;p&gt;&#123;&#123; d.username &#125;&#125;&lt;/p&gt;</span><br><span class="line">&lt;p&gt;&#123;&#123; l<span class="number">.0</span> &#125;&#125;&lt;/p&gt;</span><br><span class="line">&lt;p&gt;&#123;&#123; d.hobby<span class="number">.3</span>.info &#125;&#125;&lt;/p&gt;</span><br><span class="line"><span class="comment"># 即可以点键也可以点索引 还可以两者混用</span></span><br></pre></td></tr></table></figure>

<h2 id="二、模板语法之过滤器"><a href="#二、模板语法之过滤器" class="headerlink" title="二、模板语法之过滤器"></a>二、模板语法之过滤器</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 什么是模板语法过滤器</span><br><span class="line">	过滤器就是类似于模板语法内置的内置方法</span><br><span class="line">    django内置有<span class="number">60</span>多个过滤器 我们不需要学这么多 了解<span class="number">10</span>个左右就差不多了 后面碰到了再去记忆</span><br><span class="line">    ps: 过滤器最多只有两个参数</span><br><span class="line">    </span><br><span class="line"><span class="number">2.</span> 基本语法</span><br><span class="line">	&#123;&#123;数据|过滤器:参数&#125;&#125;</span><br><span class="line">    </span><br><span class="line"><span class="number">3.</span> 转义</span><br><span class="line">	<span class="comment"># 前端</span></span><br><span class="line">		|safe</span><br><span class="line">	<span class="comment"># 后端</span></span><br><span class="line">		<span class="keyword">from</span> django.utils.safestring <span class="keyword">import</span> mark_safe</span><br><span class="line">  			res = mark_safe(<span class="string">&#x27;&lt;h1&gt;新新&lt;/h1&gt;&#x27;</span>)</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    以后你在全栈项目的时候 前端代码不一定非要在前端页面书写</span></span><br><span class="line"><span class="string">    也可以现在先在后端写好 然后传递给前端页面</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    </span><br><span class="line"><span class="number">4.</span> 过滤器介绍</span><br><span class="line">    &lt;h1&gt;过滤器&lt;/h1&gt;</span><br><span class="line">    &lt;p&gt;统计长度:&#123;&#123; s|length &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;p&gt;默认值(第一个参数布尔值是<span class="literal">True</span>就展示第一个参数的值否在展示冒号后面的值):&#123;&#123; b|default:<span class="string">&#x27;啥也不是&#x27;</span> &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;p&gt;文件大小:&#123;&#123; file_size|filesizeformat &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;p&gt;日期格式化:&#123;&#123; current_time|date:<span class="string">&#x27;Y-m-d H:i:s&#x27;</span> &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;p&gt;切片操作(支持步长):&#123;&#123; l|<span class="built_in">slice</span>:<span class="string">&#x27;0:4:2&#x27;</span> &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;p&gt;切取字符(包含三个点):&#123;&#123; info|truncatechars:<span class="number">9</span> &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;p&gt;切取单词(不包含三个点 按照空格切):&#123;&#123; egl|truncatewords:<span class="number">9</span> &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;p&gt;切取单词(不包含三个点 按照空格切):&#123;&#123; info|truncatewords:<span class="number">9</span> &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;p&gt;移除特定的字符:&#123;&#123; msg|cut:<span class="string">&#x27; &#x27;</span> &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;p&gt;拼接操作:&#123;&#123; l|join:<span class="string">&#x27;$&#x27;</span> &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;p&gt;拼接操作(加法):&#123;&#123; n|add:<span class="number">10</span> &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;p&gt;拼接操作(加法):&#123;&#123; s|add:msg &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;p&gt;转义:&#123;&#123; hhh|safe &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;p&gt;转义:&#123;&#123; sss|safe &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &lt;p&gt;转义:&#123;&#123; res &#125;&#125;&lt;/p&gt;</span><br></pre></td></tr></table></figure>

<h2 id="三、模板语法之标签"><a href="#三、模板语法之标签" class="headerlink" title="三、模板语法之标签"></a>三、模板语法之标签</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>， 什么是标签</span><br><span class="line">	别被这个名词迷惑了 其实就是逻辑</span><br><span class="line">    </span><br><span class="line"><span class="number">2.</span> <span class="keyword">for</span>循环 标签</span><br><span class="line">	ps: 有一个快速写出<span class="keyword">for</span>循环的方法就是 写个<span class="keyword">for</span> 然后tab键</span><br><span class="line">  		&#123;% <span class="keyword">for</span> foo <span class="keyword">in</span> l %&#125;</span><br><span class="line">     		&lt;p&gt;&#123;&#123; forloop &#125;&#125;&lt;/p&gt;</span><br><span class="line">	    &#123;% endfor %&#125;</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">&#123;&#x27;parentloop&#x27;: &#123;&#125;, &#x27;counter0&#x27;: 0, &#x27;counter&#x27;: 1, &#x27;revcounter&#x27;: 3, &#x27;revcounter0&#x27;: 2, &#x27;first&#x27;: True, &#x27;last&#x27;: False&#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&#123;&#x27;parentloop&#x27;: &#123;&#125;, &#x27;counter0&#x27;: 1, &#x27;counter&#x27;: 2, &#x27;revcounter&#x27;: 2, &#x27;revcounter0&#x27;: 1, &#x27;first&#x27;: False, &#x27;last&#x27;: False&#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">这是forloop 显示出来的页面 我们只需看懂counter0 &amp; counter &amp; first &amp; last即可</span></span><br><span class="line"><span class="string">couter0:是相当于循环顺序的索引</span></span><br><span class="line"><span class="string">couter:是循环顺序的次数</span></span><br><span class="line"><span class="string">first: 如果是第一次循环则显示True</span></span><br><span class="line"><span class="string">last: 如果是最后一次循环则显示True</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">		</span><br><span class="line"><span class="number">3.</span> <span class="keyword">if</span> 判断 标签</span><br><span class="line">	&#123;% <span class="keyword">if</span> b %&#125;</span><br><span class="line">        &lt;p&gt;hi&lt;/p&gt;</span><br><span class="line">        &#123;% <span class="keyword">elif</span> s%&#125;</span><br><span class="line">            &lt;p&gt;hello&lt;/p&gt;</span><br><span class="line">        &#123;% <span class="keyword">else</span> %&#125;</span><br><span class="line">            &lt;p&gt;bye&lt;/p&gt;</span><br><span class="line">    &#123;% endif %&#125;</span><br><span class="line">   </span><br><span class="line"><span class="number">4.</span> <span class="keyword">for</span> <span class="keyword">if</span> 混用搭配</span><br><span class="line">	&#123;% <span class="keyword">for</span> foo <span class="keyword">in</span> l %&#125;</span><br><span class="line">        &#123;% <span class="keyword">if</span> forloop.first %&#125;</span><br><span class="line">            &lt;p&gt;<span class="number">1</span>&lt;/p&gt;</span><br><span class="line">            &#123;% <span class="keyword">elif</span> forloop.last%&#125;</span><br><span class="line">                &lt;p&gt;<span class="number">2</span>&lt;/p&gt;</span><br><span class="line">            &#123;% <span class="keyword">else</span> %&#125;</span><br><span class="line">                &lt;p&gt;&#123;&#123; foo &#125;&#125;&lt;/p&gt;</span><br><span class="line">        &#123;% endif %&#125;</span><br><span class="line">        &#123;% empty %&#125;</span><br><span class="line">            &lt;p&gt;<span class="keyword">for</span>循环的可迭代对象内部没有元素 根本没法循环&lt;/p&gt;</span><br><span class="line">    &#123;% endfor %&#125;</span><br><span class="line">    </span><br><span class="line"><span class="number">5.</span> <span class="keyword">for</span>循环处理字典</span><br><span class="line">	&#123;% <span class="keyword">for</span> foo <span class="keyword">in</span> d.keys %&#125;</span><br><span class="line">        &lt;p&gt;&#123;&#123; foo &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &#123;% endfor %&#125;</span><br><span class="line"></span><br><span class="line">    &#123;% <span class="keyword">for</span> foo <span class="keyword">in</span> d.values %&#125;</span><br><span class="line">        &lt;p&gt;&#123;&#123; foo &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &#123;% endfor %&#125;</span><br><span class="line"></span><br><span class="line">    &#123;% <span class="keyword">for</span> foo <span class="keyword">in</span> d.items %&#125;</span><br><span class="line">        &lt;p&gt;&#123;&#123; foo &#125;&#125;&lt;/p&gt;</span><br><span class="line">    &#123;% endfor %&#125;</span><br><span class="line">    </span><br><span class="line"><span class="number">6.</span> <span class="keyword">with</span>起别名</span><br><span class="line">	&#123;% <span class="keyword">with</span> d.hobby<span class="number">.3</span>.info <span class="keyword">as</span> nb  %&#125;</span><br><span class="line">        &lt;p&gt;&#123;&#123; nb &#125;&#125;&lt;/p&gt;</span><br><span class="line">        <span class="comment"># 在with语法内就可以通过as后面的别名快速的使用到前面非常复杂获取数据的方式</span></span><br><span class="line">        &lt;p&gt;&#123;&#123; d.hobby<span class="number">.3</span>.info &#125;&#125;&lt;/p&gt;  <span class="comment"># 也可以直接写 都可以 开心就行</span></span><br><span class="line">    &#123;% endwith %&#125;</span><br></pre></td></tr></table></figure>

<h2 id="四、自定义过滤器-标签-inclusion-tag"><a href="#四、自定义过滤器-标签-inclusion-tag" class="headerlink" title="四、自定义过滤器 标签  inclusion_tag"></a>四、自定义过滤器 标签  inclusion_tag</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 必须记住的三步曲</span><br><span class="line">	<span class="number">1.1</span> 在应用下面创建<span class="string">&#x27;templatetags&#x27;</span> 文件夹</span><br><span class="line">    <span class="number">1.2</span> 然后在这个文件夹内创建任意名字.py文件</span><br><span class="line">    <span class="number">1.3</span> 最后在文件里面输入 </span><br><span class="line">    	<span class="keyword">from</span> django <span class="keyword">import</span> templa  </span><br><span class="line">        register = templa.Library()</span><br><span class="line">       </span><br><span class="line">    </span><br><span class="line"><span class="number">2.</span> 自定义过滤器</span><br><span class="line">	<span class="comment"># 代码演示</span></span><br><span class="line"><span class="meta">        @register.filter(<span class="params">name=<span class="string">&#x27;baby&#x27;</span></span>)</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">my_sum</span>(<span class="params">v1, v2</span>):</span></span><br><span class="line">            <span class="keyword">return</span> v1 + v2</span><br><span class="line">        <span class="comment"># 前端</span></span><br><span class="line">        &#123;% load mytag %&#125;</span><br><span class="line">        &lt;p&gt;&#123;&#123; n|baby:<span class="number">666</span> &#125;&#125;&lt;/p&gt;</span><br><span class="line">        </span><br><span class="line"><span class="number">3.</span> 自定义标签 (参数可以有多个) 类似于自定义函数</span><br><span class="line">	<span class="comment"># 代码演示</span></span><br><span class="line"><span class="meta">        @register.simple_tag(<span class="params">name=<span class="string">&#x27;plus&#x27;</span></span>)</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">a,b,c,d</span>):</span></span><br><span class="line">            <span class="keyword">return</span> <span class="string">&#x27;%s-%s-%s-%s&#x27;</span>%(a,b,c,d)</span><br><span class="line">        <span class="comment"># 使用</span></span><br><span class="line">        标签多个参数彼此之间空格隔开</span><br><span class="line">        &lt;p&gt;&#123;% plus <span class="string">&#x27;jason&#x27;</span> <span class="number">123</span> <span class="number">123</span> <span class="number">123</span> %&#125;&lt;/p&gt;</span><br><span class="line"></span><br><span class="line"><span class="number">4.</span> 自定义inclusion_tag</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    内部原理</span></span><br><span class="line"><span class="string">        先定义一个方法 </span></span><br><span class="line"><span class="string">        在页面上调用该方法 并且可以传值</span></span><br><span class="line"><span class="string">        该方法会生成一些数据然后传递给一个html页面</span></span><br><span class="line"><span class="string">        之后将渲染好的结果放到调用的位置</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line"><span class="meta">    @register.inclusion_tag(<span class="params"><span class="string">&#x27;left_menu.html&#x27;</span></span>)</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">left</span>(<span class="params">n</span>):</span></span><br><span class="line">        data = [<span class="string">&#x27;第&#123;&#125;项&#x27;</span>.<span class="built_in">format</span>(i) <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(n)]</span><br><span class="line">        <span class="comment"># 第一种</span></span><br><span class="line">        <span class="comment"># return &#123;&#x27;data&#x27;:data&#125;  # 将data传递给left_menu.html</span></span><br><span class="line">        <span class="comment"># 第二种</span></span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">locals</span>()  <span class="comment"># 将data传递给left_menu.html</span></span><br><span class="line"></span><br><span class="line">    &#123;% left <span class="number">5</span> %&#125;</span><br><span class="line">    <span class="comment"># 总结:当html页面某一个地方的页面需要传参数才能够动态的渲染出来，并且在多个页面上都需要使用到该局部 那么就考虑将该局部页面做成inclusion_tag形式</span></span><br></pre></td></tr></table></figure>

<h2 id="五、模板的继承"><a href="#五、模板的继承" class="headerlink" title="五、模板的继承"></a>五、模板的继承</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">你们有没有见过一些网站</span></span><br><span class="line"><span class="string">	这些网站页面整体都大差不差 只是某一些局部在做变化	</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="number">1.</span> 代码演示</span><br><span class="line">    <span class="comment"># 模版的继承 你自己先选好一个你要想继承的模版页面</span></span><br><span class="line">    &#123;% extends <span class="string">&#x27;home.html&#x27;</span> %&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 继承了之后子页面跟模版页面长的是一模一样的 你需要在模版页面上提前划定可以被修改的区域</span></span><br><span class="line">    &#123;% block content %&#125;</span><br><span class="line">        模版内容</span><br><span class="line">    &#123;% endblock %&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 子页面就可以声明想要修改哪块划定了的区域</span></span><br><span class="line">    &#123;% block content %&#125;</span><br><span class="line">        子页面内容	</span><br><span class="line">    &#123;% endblock %&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 注意事项</span><br><span class="line">    <span class="comment"># 一般情况下模版页面上应该至少有三块可以被修改的区域</span></span><br><span class="line">        <span class="number">1.</span>css区域</span><br><span class="line">      <span class="number">2.</span>html区域</span><br><span class="line">      <span class="number">3.j</span>s区域</span><br><span class="line">      &#123;% block css %&#125;</span><br><span class="line"></span><br><span class="line">        &#123;% endblock %&#125;</span><br><span class="line"></span><br><span class="line">      &#123;% block content %&#125;</span><br><span class="line"></span><br><span class="line">        &#123;% endblock %&#125;</span><br><span class="line"></span><br><span class="line">      &#123;% block js %&#125;</span><br><span class="line"></span><br><span class="line">        &#123;% endblock %&#125;</span><br><span class="line">      <span class="comment"># 每一个子页面就都可以有自己独有的css代码 html代码 js代码</span></span><br><span class="line"></span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    一般情况下 模版的页面上划定的区域越多 那么该模版的扩展性就越高</span></span><br><span class="line"><span class="string">    但是如果太多 那还不如自己直接写</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="六、模板的导入"><a href="#六、模板的导入" class="headerlink" title="六、模板的导入"></a>六、模板的导入</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">将页面的某一个局部当成模块的形式</span></span><br><span class="line"><span class="string">哪个地方需要就可以直接导入使用即可</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">&#123;% include <span class="string">&#x27;wasai.html&#x27;</span> %&#125;</span><br></pre></td></tr></table></figure>




            </div>

            <!-- Post Comments -->
            
    <!-- 使用 DISQUS_CLICK -->
<div id="disqus-comment">
    <div id="disqus_thread"></div>

<!-- add animation -->
<style>
	.disqus_click_btn {
            line-height: 30px;
            margin: 0;
            min-width: 50px;
            padding: 0 14px;
            display: inline-block;
            font-family: "Roboto", "Helvetica", "Arial", sans-serif;
            font-size: 14px;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0;
            overflow: hidden;
            will-change: box-shadow;
            transition: box-shadow .2s cubic-bezier(.4, 0, 1, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), color .2s cubic-bezier(.4, 0, .2, 1);
            outline: 0;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            vertical-align: middle;
            border: 0;
            background: rgba(158, 158, 158, .2);
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
            color: #fff;
            background-color: #7EC0EE;
            text-shadow: 0
        }
</style>
	
<div class="btn_click_load" id="disqus_bt"> 
    <button class="disqus_click_btn">点击查看评论</button>
</div>

<!--
<script type="text/javascript">
$('.btn_click_load').click(function() {
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'http-miccall-tech'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    document.getElementById('disqus_bt').style.display = "none";
});
</script>
-->
<script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://gitee.com/AmeX/amelin-ting.git/2018/05/03/Django------%E6%A8%A1%E7%89%88%E5%B1%82/';  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'https://gitee.com/AmeX/amelin-ting.git/2018/05/03/Django------%E6%A8%A1%E7%89%88%E5%B1%82/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>

<script type="text/javascript">
    $('.btn_click_load').click(function() {  //click to load comments
        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document;
            var s = d.createElement('script');
            s.src = '//http-miccall-tech.disqus.com/embed.js';
            s.setAttribute('data-timestamp', + new Date());
            (d.head || d.body).appendChild(s);
        })();
        $('.btn_click_load').css('display','none');
    });
</script>
</div>
<style>
    #disqus-comment{
        background-color: #eee;
        padding: 2pc;
    }
</style>


        </div>
        <!-- Copyright 版权 start -->
                <div id="copyright">
            <ul>
                <li>&copy;Powered By <a target="_blank" rel="noopener" href="https://hexo.io/zh-cn/" style="border-bottom: none;">hexo</a></li>
                <li>Design: <a target="_blank" rel="noopener" href="http://miccall.tech " style="border-bottom: none;">miccall</a></li>
            </ul>
            
                <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
			
        </div>
    </div>
</body>



 	
</html>
